/* Copyright 2025 The OpenXLA Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/

#ifndef XLA_BACKENDS_GPU_RUNTIME_THUNK_PASS_PIPELINE_H_
#define XLA_BACKENDS_GPU_RUNTIME_THUNK_PASS_PIPELINE_H_

#include <cstdint>
#include <memory>
#include <string>
#include <utility>
#include <vector>

#include "absl/base/nullability.h"
#include "absl/status/statusor.h"
#include "absl/strings/string_view.h"
#include "xla/backends/gpu/runtime/sequential_thunk.h"
#include "xla/hlo/ir/hlo_module.h"
#include "xla/service/buffer_assignment.h"
#include "xla/stream_executor/device_description.h"
#include "xla/xla.pb.h"

namespace xla {
namespace gpu {

class ThunkPassBufferAllocator {
 public:
  virtual ~ThunkPassBufferAllocator() = default;

  // Creates a new allocation of the provided size.
  // The ownership of the returned pointer stays with the
  // ThunkPassBufferAllocator object.
  virtual absl::StatusOr<BufferAllocation* absl_nonnull> NewEmptyAllocation(
      int64_t size) = 0;
};

class ThunkPassInterface {
 public:
  virtual ~ThunkPassInterface() = default;

  virtual absl::StatusOr<bool> Run(SequentialThunk* root_thunk,
                                   const DebugOptions& debug_options,
                                   const HloModule* absl_nullable hlo_module,
                                   const se::DeviceDescription& device_info,
                                   ThunkPassBufferAllocator& allocator) = 0;

  virtual absl::string_view name() const = 0;
};

class ThunkPassPipeline : public ThunkPassInterface {
 public:
  explicit ThunkPassPipeline(absl::string_view name) : name_(name) {}

  void AddPass(std::unique_ptr<ThunkPassInterface> pass) {
    passes_.push_back(std::move(pass));
  }

  absl::string_view name() const override { return name_; }

  // Runs all optimization passes on the given thunk sequence.
  // Returns true if any pass changed the thunk tree.
  absl::StatusOr<bool> Run(SequentialThunk* root_thunk,
                           const DebugOptions& debug_options,
                           const HloModule* absl_nullable hlo_module,
                           const se::DeviceDescription& device_info,
                           ThunkPassBufferAllocator& allocator) override;

 private:
  std::string name_;
  std::vector<std::unique_ptr<ThunkPassInterface>> passes_;
};

}  // namespace gpu
}  // namespace xla

#endif  // XLA_BACKENDS_GPU_RUNTIME_THUNK_PASS_PIPELINE_H_
